library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity COPY is
	Port ( v : in STD_LOGIC_VECTOR (15 downto 0);
		vin : in STD_LOGIC;
		vack : out STD_LOGIC;
		clkin : in STD_LOGIC;
		a : out STD_LOGIC_VECTOR (15 downto 0);
		astr : out STD_LOGIC;
		aack : in STD_LOGIC;
		b : out STD_LOGIC_VECTOR (15 downto 0);
		bstr : out STD_LOGIC;
		back : in STD_LOGIC);
	end COPY;
architecture Behavioral of COPY is
	type estados is(inicio, envio);
	begin
		dados: process(clkin, vin, aack, back)
			variable estado: estados := inicio;
			variable enva: STD_LOGIC := '0';
			variable envb: STD_LOGIC := '0';
			variable vd: STD_LOGIC_VECTOR(15 downto 0);
		begin
			if clkin'event and clkin = '1' then
				a <= "ZZZZZZZZZZZZZZZZ";
				b <= "ZZZZZZZZZZZZZZZZ";
				astr <= '0';
				bstr <= '0';
				vack <= '0';
				case estado is
					when inicio =>
						if vin = '1' then
							vd := v;
							vack <= '1';
							enva := '0';
							envb := '0';
							estado := envio;
						end if;
					when envio =>
						if aack = '1' then
							enva := '1';
						end if;
						if enva = '0' then
							a <= vd;
							astr <= '1';
						end if;
						if back = '1' then
							envb := '1';
						end if;
						if envb = '0' then
							b <= vd;
							bstr <= '1';
						end if;
						if enva = '1' and envb = '1' then
							estado := inicio;
						end if;
				end case;
			end if;
		end process dados;
end Behavioral;